
*All raw data files are from 2012 folders.
* 1 Bring in LBD. We use LBD to calculate plant age and for employment, which is
* used in the construction of value added per worker and for plant weights.
local yy "12"
use ${data}/lbd_c15_`yy'.dta, clear
capture drop mfsic1 mfsic4 flaga sic pdiv bestsic cenflag flag_i cbp_sic cbp_bestsic cbp_naics cbp_bestnaics cbp
ren ( emp pay naics) ( emp_lbd payroll_lbd naics_lbd)
cap gen T=2012
compress
save ${data}lbd_c15_`yy'.dta, replace


* 2 Bring in CMF main file
tempfile cmfA
use ${data}/cmf_12.dta, clear
keep lbdnum_c201500 survu_id cou fipsst cmsa msa va te tvs firmid
ren (te tvs) (te_A tvs_A)
save `cmfA'

/* 3 Bring in CMF TFP file. 
The TFPr variable contained in this file is constructed by Foster, 
Grim, and Haltiwanger (2016) and updated in Decker, Haltiwanger, Jarmin, 
and Miranda (forthcoming) through 2013 and covering our 2012 CMF wave. 
(See also our data description in paper.)*/
use ${data}/cmf_12_tfp.dta, clear
egen tag_id = tag(survu_id)
keep if tag_id
drop tag_id

capture drop sic bab bae fib fie mab mae tab tae wib wie ///
lbdnum_orig_txt sic_bea sic_bls lbdnum_org flag_fix_lbdnum imp_lbdnum ///
lbdnum_c201002 lbdnum_c201100 lbdnum_c201101 lbdnum_c201200 lbdnum_c201300 ///
lbdnum_c201400

merge 1:1 survu_id using `cmfA'
keep if _m==3

*Drop if there is no lbd number becuase we cannot determine plant age
drop if lbdnum == "" 
ren (year lbdnum_c201500) (T lbdnum)

cap drop _m
merge 1:1 lbdnum using ${data}/lbd_c15_12.dta
keep if _m==3
drop _m

gen fipsstco =  state + county if state!="" & county!=""
replace fipsstco = fipsst + cou if state=="" | county==""

destring fipsstco, force replace
drop if fipsstco==.
ren msa msa_lbd
ren cmsa cmsa_lbd

cap drop _m
*This crosswalk file is the same as "list1.xls" in the CBP data and code folder
merge m:1 fipsstco using ${data}/county_to_cbsa_crosswalk.dta
drop if _m!=3

gen age = 2012-firstyear
ren naics naics_cmf
keep lbdnum T bestnaics naics_cmf fipsstco cbsa msa_lbd te emp_lbd va ltfp age  tvs q sw ww payroll_lbd  firmid
gen lvapw = log(va/emp_lbd)
gen lspw=log(tvs/emp_lbd)

save ${data}/cmf_lbd_cbsa_tfp_12.dta, replace

